package buildrobot_info

import (
	"go_server/game_server/api/service/bag"
	"go_server/game_server/api/service/build"
	"go_server/game_server/api/service/item"
	"go_server/game_server/protocol/pb"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
	"strconv"
)

// 2000 获得所有建造中的机器人
func (s *Buildrobot_info_service) GM_GetAllBuildRobots(conn ziface.IConnection, roleid int64, params []string) pb.ErrorCodeManager {
	msg := &pb.GetAllBuildRobotsC2S{}
	Buildrobot_info_serviceObj.S2C_CheckBuildRobotInfoByBuildUnlock(conn, roleid)
	Buildrobot_info_serviceObj.S2C_GetAllBuildRobots(conn, roleid, msg)
	return pb.ErrorCodeManager_SUCCESS
}

// 2001 添加建造机器人
func (s *Buildrobot_info_service) GM_AddBuildRobotLvUp(conn ziface.IConnection, roleid int64, params []string) pb.ErrorCodeManager {
	msg := &pb.AddRobot2BuildLvUpC2S{
		Robotserids: make([]int32, 1),
	}
	if v, err := strconv.Atoi(params[0]); err == nil {
		msg.Robotserids[0] = int32(v)
	}
	if v, err := strconv.Atoi(params[1]); err == nil {
		msg.Buildmapid = int32(v)
	}
	if v, err := strconv.Atoi(params[2]); err == nil {
		msg.Buildcfgid = int32(v)
	}
	if v, err := strconv.Atoi(params[3]); err == nil {
		msg.Buildserid = int32(v)
	}
	Buildrobot_info_serviceObj.S2C_AddBuildRobotLvUp(conn, roleid, msg)
	return pb.ErrorCodeManager_SUCCESS
}

// 2002 删除建造机器
func (s *Buildrobot_info_service) GM_DelBuildRobotLvUp(conn ziface.IConnection, roleid int64, params []string) pb.ErrorCodeManager {
	msg := &pb.DelRobot2BuildLvUpC2S{
		Robotserids: make([]int32, 1),
	}
	if v, err := strconv.Atoi(params[0]); err == nil {
		msg.Robotserids[0] = int32(v)
	}
	if v, err := strconv.Atoi(params[1]); err == nil {
		msg.Buildmapid = int32(v)
	}
	if v, err := strconv.Atoi(params[2]); err == nil {
		msg.Buildcfgid = int32(v)
	}
	if v, err := strconv.Atoi(params[3]); err == nil {
		msg.Buildserid = int32(v)
	}
	Buildrobot_info_serviceObj.S2C_DelBuildRobotLvUp(conn, roleid, msg)
	return pb.ErrorCodeManager_SUCCESS
}

// 2003 检测是否有升级完成的建筑
func (s *Buildrobot_info_service) GM_CheckBuildRobotLvUpOver(conn ziface.IConnection, roleid int64) pb.ErrorCodeManager {
	Buildrobot_info_serviceObj.S2C_BuildRobotLvUpOver(conn, roleid)
	return pb.ErrorCodeManager_SUCCESS
}

// 2004 删除所有建造中的机器
func (s *Buildrobot_info_service) GM_DelAllBuildRobotLvUp(conn ziface.IConnection, roleid int64, params []string) pb.ErrorCodeManager {
	msg := &pb.DelAllRobot2BuildLvUpC2S{}
	Buildrobot_info_serviceObj.S2C_DelAllRobot2BuildLvUp(conn, roleid, msg)
	return pb.ErrorCodeManager_SUCCESS
}

// 2007 指定建造中的建筑升级的总共升级时间(秒)
func (s *Buildrobot_info_service) GM_SetBuildRobotLvUpTime(conn ziface.IConnection, roleid int64, params []string) pb.ErrorCodeManager {

	buildmapid := int32(0)
	buildcfgid := int32(0)
	buildserid := int32(0)
	upgradeTime := int32(0)

	if v, err := strconv.Atoi(params[1]); err == nil {
		buildmapid = int32(v)
	}
	if v, err := strconv.Atoi(params[2]); err == nil {
		buildcfgid = int32(v)
	}
	if v, err := strconv.Atoi(params[3]); err == nil {
		buildserid = int32(v)
	}
	if v, err := strconv.Atoi(params[4]); err == nil {
		upgradeTime = int32(v)
	}

	bid := build.CombinBuildKey(roleid, buildmapid, buildcfgid, buildserid)

	robot := s.LoadByRoleId(roleid)
	if robot != nil {
		if bv, ok := robot.BuildData[bid]; ok {
			bv.EndMilliTime = bv.StartMilliTime + int64(upgradeTime*1000)

			msgS2C := &pb.AddRobot2BuildLvUpS2C{}
			msgS2C.Builds = make([]*pb.BuildRobotLvUpData, 1)
			msgS2C.Builds[0] = bv.ToPb_BuildRobotLvUpData(robot.GetRobotBySerid)

			if msgS2C != nil {
				zlog.Debugf("ProtocolManager_AddBuildRobotLvUp %v", msgS2C)
				defer func() {
					conn.SendBuffPBMsg(uint32(pb.ProtocolManager_AddBuildRobotLvUp), msgS2C)
				}()
			}
		}

		s.S2C_BuildRobotUpgradeSuc(conn, robot)
	}
	return pb.ErrorCodeManager_SUCCESS
}

// 2008 指定建造中的建筑升级成功
func (s *Buildrobot_info_service) GM_BuildRobotLvUpOverSuc(conn ziface.IConnection, roleid int64, params []string) pb.ErrorCodeManager {
	buildmapid := int32(0)
	buildcfgid := int32(0)
	buildserid := int32(0)
	if v, err := strconv.Atoi(params[1]); err == nil {
		buildmapid = int32(v)
	}
	if v, err := strconv.Atoi(params[2]); err == nil {
		buildcfgid = int32(v)
	}
	if v, err := strconv.Atoi(params[3]); err == nil {
		buildserid = int32(v)
	}
	bid := build.CombinBuildKey(roleid, buildmapid, buildcfgid, buildserid)
	robot := s.LoadByRoleId(roleid)
	if robot != nil {
		if bv, ok := robot.BuildData[bid]; ok {
			bv.EndMilliTime = bv.StartMilliTime
			bv.RegisterCountDownCheck_BuildRobotLvUpOver()
		}
		s.S2C_BuildRobotUpgradeSuc(conn, robot)
	}
	return pb.ErrorCodeManager_SUCCESS
}

// 2009 所有建造中的建筑升级成功
func (s *Buildrobot_info_service) GM_AllBuildRobotLvUpOverSuc(conn ziface.IConnection, roleid int64, params []string) pb.ErrorCodeManager {
	robot := s.LoadByRoleId(roleid)
	if robot != nil && robot.BuildData != nil {
		for _, bv := range robot.BuildData {
			bv.EndMilliTime = bv.StartMilliTime
			bv.RegisterCountDownCheck_BuildRobotLvUpOver()
		}
		s.S2C_BuildRobotUpgradeSuc(conn, robot)
	}
	return pb.ErrorCodeManager_SUCCESS
}

func (s *Buildrobot_info_service) GM_BuildRobotChargeByItem(conn ziface.IConnection, roleid int64, params []string) pb.ErrorCodeManager {
	robot := s.LoadByRoleId(roleid)
	if robot != nil {
		msgS2C := &pb.BuildRobotChargeByItemC2S{}
		msgS2C.Charges = make([]*pb.BuildRobotChargeByItemC2S_BRC, 0)

		charges := item.GetRobotStrengthItems()
		if charges != nil {
			for _, rv := range robot.RobotData {
				brc := &pb.BuildRobotChargeByItemC2S_BRC{
					RobotSerid: rv.Serid,
					Items:      make(map[int32]int32),
				}
				for _, v := range charges {
					brc.Items[v] = 1
					msgS2C.Charges = append(msgS2C.Charges, brc)

					bag.AddItemSingle(robot.RoleId, v, 1)
				}
			}
		}

		return s.S2C_BuildRobotChargeByItem(conn, roleid, msgS2C)
	}
	return pb.ErrorCodeManager_SUCCESS
}
